layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 库存表管理
     */
    var StorageStock = {
        tableId: "storageStockTable"
    };

    /**
     * 初始化表格的列
     */
    StorageStock.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'ID'},
            {field: 'operateId',hide: true, sort: true, title: 'Branch ID'},
            {field: 'operateName', sort: true, title: 'Branch Name'},
            {field: 'customerId',hide: true, sort: true, title: 'Customer ID'},
            {field: 'customerName', sort: true, title: 'Customer Name'},
            {field: 'warehouseId',hide: true, sort: true, title: 'Warehouse ID'},
            {field: 'warehouseName', sort: true, title: 'Warehouse Name'},
            {field: 'areaId',hide: true, sort: true, title: 'Area ID'},
            {field: 'areaNumber', sort: true, title: 'Area No'},
            {field: 'positionId', hide: true,sort: true, title: 'Location ID'},
            {field: 'positionNumber', sort: true, title: 'Location No'},
            {field: 'trayId',hide: true, sort: true, title: 'Tray ID'},
            {field: 'trayNumber', sort: true, title: 'Tray No'},
            {field: 'goodsId',hide: true, sort: true, title: 'Product ID'},
            {field: 'goodsName', sort: true, title: 'Product Name'},
            {field: 'goodsNumber', sort: true, title: 'Product No'},
            {field: 'num', sort: true, title: 'Quantity'},
            {field: 'balenum', sort: true, title: 'Carton Qty'},
            {field: 'canNum', sort: true, title: 'Available Qty'},
            {field: 'stockWeight', sort: true, title: 'Stock Weight'},
            {field: 'stockVolume', sort: true, title: 'Stock Volume'},
            {field: 'unitprice', sort: true, title: 'Unit Price'},
            {field: 'stockMoney', sort: true, title: 'Stock Amount'},
            {field: 'deliverId',hide: true, sort: true, title: 'Delivery ID'},
            {field: 'deptId',hide: true, sort: true, title: 'Department ID'},
            {field: 'version',hide: true, sort: true, title: 'Version'},
            {field: 'createTime',hide: true, sort: true, title: 'Create Time'},
            {field: 'updateTime',hide: true, sort: true, title: 'Update Time'},
            {field: 'createUser',hide: true, sort: true, title: 'Create User'},
            {field: 'updateUser',hide: true, sort: true, title: 'Update User'},
            {align: 'center', toolbar: '#tableBar', title: 'Action'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    StorageStock.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(StorageStock.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
                 // 搜索按钮点击事件
                 $('#btnSearch').click(function () {
                     StorageStock.search();
                 });
                 // 添加按钮点击事件
                 $('#btnAdd').click(function () {
                     StorageStock.openAddDlg();
                 });
                 // 导出excel
                 $('#btnExp').click(function () {
                     StorageStock.exportExcel();
                 });
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    StorageStock.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/storageStock/add';
    };

    /**
     * 导出excel按钮
     */
    StorageStock.exportExcel = function () {
        var checkRows = table.checkStatus(StorageStock.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("Please select data to export");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    StorageStock.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/storageStock/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    StorageStock.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/storageStock/delete", function (data) {
                Feng.success("Delete successful!");
                table.reload(StorageStock.tableId);
            }, function (data) {
                Feng.error("Delete failed!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("Are you sure to delete?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + StorageStock.tableId,
        url: Feng.ctxPath + '/storageStock/list',
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: StorageStock.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        StorageStock.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        StorageStock.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        StorageStock.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + StorageStock.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            StorageStock.openEditDlg(data);
        } else if (layEvent === 'delete') {
            StorageStock.onDeleteItem(data);
        }
    });
});
